<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\controller\Common;
//class Goods extends Common
class Goods extends Controller
{
    public function goodsList()
    {
        $adminId = session('uid','','admin');
        $list = db('goods')->where('is_del',0);
        $list = $list->order('status DESC ,id desc')->paginate(10);
        $this->assign([
            'list'=>$list,
        ]);
        return view();
    }
    
    public function goodsAdd()
    {
        if(request()->isAjax())
        {
            $data = input('post.')['data'];
            $data['admin_id'] = session('uid','','admin');
            $data['add_time'] = time();
            $sqlStatus = db('goods')->insert($data);
            if($sqlStatus  )
            {
                return ['msg'=>'操作成功','icon'=>1];
            }
            else
            {
                return ['msg'=>'操作失败','icon'=>2];
            }
        }
        return view();
    }
    
    public function goodsEdit()
    {
        if(input('id'))
        {
            if(request()->isAjax())
            {
                $data = input('post.')['data'];
                $data['id'] = input('id');
                $sqlStatus = db('goods')->update($data);
                if($sqlStatus  )
                {
                    return ['msg'=>'操作成功','icon'=>1];
                }
                else
                {
                    return ['msg'=>'操作失败','icon'=>2];
                }
            }
            $info = db('goods')->find(input('id'));
            $this->assign([
                'info'=>$info,
            ]);
            return view('goods_add');
        }
    }
    
    public function countAdd()
    {
        if(input('id'))
        {
            if(request()->isAjax())
            {
                $data = input('post.')['data'];
               
                $data['admin_id'] = session('uid','','admin');
                $data['goods_id'] = input('id');
                $data['status'] = 1;
                $data['type'] = 1;
                $data['old_stock'] = db('goods')->where('id', input('id'))->value('stock');
                $data['add_time'] = time();
                
                $sqlStatus = db('goods')
                            ->where('id', input('id'))
                            ->update([
                            'goods_count' => ['inc', $data['count']],
                            'stock' => ['inc', $data['count']]
                            ]);
                
                $sqlStatus = db('goods_orders')->insert($data);
                if($sqlStatus  )
                {
                    return ['msg'=>'操作成功','icon'=>1];
                }
                else
                {
                    return ['msg'=>'操作失败','icon'=>2];
                }
            }
            $info = db('goods')->find(input('id'));
            $this->assign([
                'info'=>$info,
            ]);
            return view();
        }
    }

    public function goodsDel()
    {
        if(input('id'))
        {
            $sqlStatus = db('goods')->where('id',input('id'))->setField('is_del', '1');;
            if($sqlStatus)
            {
                return ['msg'=>'操作成功','icon'=>1];
            }
            else
            {
                return ['msg'=>'操作失败','icon'=>2];
            }
        }
    }
    
    public function costTotal()
    {
        $goodsId = input('goods_id');
        $start_date = input('start_date');
        $end_date = input('end_date');
        $adminId  = input('admin_id');

        $goods = db('goods')->where('status',1)->order('id DESC');
        if($goodsId)
            $goods = $goods->where('id',$goodsId);
        $goods = $goods->select();
        foreach ($goods as $k=>$v)
        {
            $goods[$k]['count'] = db('goods_orders')->where('goods_id',$v['id'])->where('status',1)->where('type',2);
            if($start_date)
                $goods[$k]['count'] = $goods[$k]['count']->whereTime("add_time",'>=',$start_date);
            if($end_date)
                $goods[$k]['count'] = $goods[$k]['count']->whereTime("add_time",'<=',$end_date.' 23:59:59');
            if($adminId)
                $goods[$k]['count'] = $goods[$k]['count']->where("admin_id",$adminId);
            $goods[$k]['count'] = $goods[$k]['count']->count();
        }
        $adminList = db('admin')->where('status',1)->where('group_id','IN','5,2')->order('id DESC')->select();
        $this->assign([
            'goods'=>$goods,
            'adminList'=>$adminList
        ]);
        return view();
    }
    
    
}